﻿using AutoMapper;
using Dm;
using MediatR;
using Microsoft.EntityFrameworkCore;
using ProductionManagement.Application.Command.MaterialCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Domain.ProductionDomain;
using ProductionManagement.Infrstructre.lmp;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Pipelines;
using System.Runtime.InteropServices.JavaScript;

namespace ProductionManagement.Application.CommandHandler.MaterialCommandHandlers
{
    public class MaterialBlikCommandHandlers : IRequestHandler<MaterialBlikCommand, List<MaterialBlikDto>>
    {
        private readonly IMaterialRepository _materialRepository;
        private readonly IItemSubpiecesRepository _itemSubpiecesRepository;
        private readonly ISqlSugarClient _db;
        private readonly IMapper _mapper;

        public MaterialBlikCommandHandlers(IMaterialRepository materialRepository,
                                           IItemSubpiecesRepository itemSubpiecesRepository,
                                           ISqlSugarClient db,
                                           IMapper mapper)
        {
            _materialRepository = materialRepository;
            _itemSubpiecesRepository = itemSubpiecesRepository;
            _db = db;
            _mapper = mapper;
        }

        public async Task<List<MaterialBlikDto>> Handle(MaterialBlikCommand request, CancellationToken cancellationToken)
        {
            var list= (from a  in await _materialRepository.GetAll() select a).Where(x => x.MaterialId == request.MaterialId).ToList();
            var item = (from b in await _itemSubpiecesRepository.GetAll() select b).ToList();
            var result = (from c in list
                          select new MaterialBlikDto
                          {
                            
                              MaterialId = c.MaterialId,
                              MaterialNode = c.MaterialNode,
                              MSubject = c.MSubject,
                              MNumber = c.MNumber,
                              MWatchmakers = c.MWatchmakers,
                              MRemark = c.MRemark,
                              ProductId=c.ProductId,
                              PName = c.MPName,
                              PNumber = c.MPNumber,
                              MPSpec = c.MPSpec,
                              MPUnit = c.MPUnit,
                              MQuantity = c.MQuantity,
                              MProductPersonas = c.MProductPersonas,
                              MProcess = c.MProcess,
                              MWearRate = c.MWearRate,
                              AddName = c.AddName,
                              AddTime = c.AddTime,
                              Office_Id = c.Office_Id,
                              User_Id = c.User_Id,
                              ItemSubpiecesList = (from d in item
                                                   where d.MaterialNode == c.MaterialNode
                                                   select new ItemSubpiece
                                                   {
                                                       ItemSubpieceId = d.ItemId,
                                                       ProductId = d.ProductId,
                                                       IProductPersonas=d.IProductPersonas,
                                                       INumber = d.INumber,
                                                       PNumber=d.PNumber,
                                                       PName=d.PName,
                                                       PSpec=d.PSpec,
                                                       PUnit=d.PUnit,
                                                       ProductRatio=d.ProductRatio,
                                                       IProcess=d.IProcess,
                                                       IWearRate=d.IWearRate,
                                                       IPickingMethod=d.IPickingMethod,
                                                       MaterialNode=d.MaterialNode,
                                                       IMRemark=d.IMRemark,
                                                       AddName=d.AddName,
                                                       AddTime=d.AddTime,
                                                       Office_Id=d.Office_Id,
                                                       User_Id=d.User_Id
                                                   }).ToList()




                          });
            return result.ToList();
               
        }
    }

}
